package com.training.service;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Page;
import com.training.common.model.Result;
import com.training.common.model.Student;
import com.training.common.model.Subject;
import com.training.common.model.Train;

import java.util.ArrayList;
import java.util.List;

public class TrainServiceImp implements TrainService {

    @Inject(StudentServiceImp.class)
    private StudentService studentService;

    @Inject(SubjectServiceImp.class)
    private SubjectService subjectService;

    @Override
    public Page<Train> getTrainPage(int pageNumber, int pageSize, String name) {
        return Train.dao.getTrainPage(pageNumber,pageSize,name);
    }

    @Override
    public Page<Student> getTrainResultPage(int train_id,int pageNumber, int pageSize,String number, String name) {
        Train train = Train.dao.findById(train_id);
        Page<Student> studentPage = studentService.getStudentTrainPage(train.getClassids(),pageNumber, pageSize,number, name);
        List<Subject> subjectList = subjectService.getTrainSubjectList(train.getSubjectids());
        List<Student> studentList = studentPage.getList();
        List<Student> newStudentList = new ArrayList<Student>();
        for(Student student : studentList){
            student.put("newid",train_id+"-"+student.getId());
            for (Subject subject : subjectList){
                Result resultmodel = Result.dao.getTrainResult(train_id,subject.getId(),student.getClassId(),student.getId());
                String result = "";
                if(resultmodel == null){
                    result = "无";
                }else{
                    result = resultmodel.getResult();
                }
                student.put(subject.getId()+"",result);
            }
            newStudentList.add(student);
        }
        studentPage.setList(newStudentList);
        return studentPage;
    }

    @Override
    public boolean deleteTrainResult(String[] ids) {
        boolean boo = true;
        List<Result> resultList = Result.dao.getTrainResult(Integer.parseInt(ids[0]),Integer.parseInt(ids[1]));
        for(Result result : resultList){
            boo = result.delete();
        }
        return boo;
    }

}
